package com.cskaoyan.service.impl;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.coupon.*;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.MarketCoupon;
import com.cskaoyan.bean.po.MarketCouponExample;
import com.cskaoyan.bean.po.MarketCouponUser;
import com.cskaoyan.bean.po.MarketCouponUserExample;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import com.cskaoyan.service.CouponService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author chenai
 * @description TODO
 * @date 2022-06-06 17:19
 */
@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired
    ObjectMapper objectMapper;

    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;

    @Override
    public BaseRespVo list(BaseParam baseParam, String name, Short type) {
        List<MarketCoupon> marketCoupon = new ArrayList<>();
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (type != null) {
            criteria.andTypeEqualTo(type);
        }
        criteria.andDeletedEqualTo(false);
        marketCouponExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        marketCoupon = marketCouponMapper.selectByExample(marketCouponExample);
        PageInfo pageInfo = new PageInfo(marketCoupon);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return BaseRespVo.ok(new CouponListVO(baseParam.getPage(), baseParam.getLimit(), total, pages, marketCoupon));
    }

    @Override
    public BaseRespVo create(CouponCreateBO coupleCreateBO) {
        Date date = new Date();
        MarketCoupon marketCoupon = new MarketCoupon(null, coupleCreateBO.getName(), coupleCreateBO.getDesc(), coupleCreateBO.getTag(), Integer.parseInt(coupleCreateBO.getTotal()), new BigDecimal(coupleCreateBO.getDiscount()),
                new BigDecimal(coupleCreateBO.getMin()), coupleCreateBO.getLimit(), coupleCreateBO.getType(), Short.parseShort(coupleCreateBO.getStatus()), coupleCreateBO.getGoodsType(),
                "[" + String.join(",", coupleCreateBO.getGoodsValue()) + "]", null, coupleCreateBO.getTimeType(), Short.parseShort(coupleCreateBO.getDays()), coupleCreateBO.getStartTime(), coupleCreateBO.getEndTime(),
                date, date, false);
        if (marketCoupon.getTimeType() == 0) {
            Short days = marketCoupon.getDays();
            if (days == 0) {
                return BaseRespVo.invalidParameter("请输入天数");
            }
            marketCoupon.setStartTime(date);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE,days);
            marketCoupon.setEndTime(calendar.getTime());
        }else {
            if(marketCoupon.getStartTime() == null){
                marketCoupon.setStartTime(date);
            }
            if (marketCoupon.getEndTime() == null) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.add(Calendar.YEAR, 99);
                marketCoupon.setEndTime(calendar.getTime());
            }
            if(marketCoupon.getStartTime().after(marketCoupon.getEndTime())){
                return BaseRespVo.invalidParameter("开始时间不能大于结束时间");
            }
        }
        marketCouponMapper.insert(marketCoupon);
        if (coupleCreateBO.getGoodsType() != null) {
            String[] strings = new String[0];
            try {
                strings = objectMapper.readValue(marketCoupon.getGoodsValue(), String[].class);
                ArrayList<String> stringList = new ArrayList<>(Arrays.asList(strings));
                return BaseRespVo.ok(new CouponCreateVO(marketCoupon.getId(), marketCoupon.getName(), marketCoupon.getDesc(), marketCoupon.getTag(), marketCoupon.getTotal(), marketCoupon.getDiscount(),
                        marketCoupon.getMin(), marketCoupon.getLimit(), marketCoupon.getType(), marketCoupon.getStatus(), marketCoupon.getGoodsType(), stringList, marketCoupon.getCode(), marketCoupon.getTimeType(), marketCoupon.getDays(),
                        marketCoupon.getStartTime(), marketCoupon.getEndTime(), marketCoupon.getAddTime(), marketCoupon.getUpdateTime(), marketCoupon.getDeleted()));
            } catch (IOException e) {
                return BaseRespVo.invalidParameter("参数错误");
            }
        } else {
            return BaseRespVo.ok(new CouponCreateVO(marketCoupon.getId(), marketCoupon.getName(), marketCoupon.getDesc(), marketCoupon.getTag(), marketCoupon.getTotal(), marketCoupon.getDiscount(),
                    marketCoupon.getMin(), marketCoupon.getLimit(), marketCoupon.getType(), marketCoupon.getStatus(), marketCoupon.getGoodsType(), null, marketCoupon.getCode(), marketCoupon.getTimeType(), marketCoupon.getDays(),
                    marketCoupon.getStartTime(), marketCoupon.getEndTime(), marketCoupon.getAddTime(), marketCoupon.getUpdateTime(), marketCoupon.getDeleted()));
        }


    }

    @Override
    public BaseRespVo update(CouponUpdateBO couponUpdateBO) {
        MarketCoupon marketCoupon = new MarketCoupon(couponUpdateBO.getId(), couponUpdateBO.getName(), couponUpdateBO.getDesc(), couponUpdateBO.getTag(), couponUpdateBO.getTotal(), new BigDecimal(couponUpdateBO.getDiscount()),
                couponUpdateBO.getMin(), couponUpdateBO.getLimit(), couponUpdateBO.getType(), couponUpdateBO.getStatus(), couponUpdateBO.getGoodsType(),
                "[" + String.join(",", couponUpdateBO.getGoodsValue()) + "]", couponUpdateBO.getCode(), couponUpdateBO.getTimeType(), couponUpdateBO.getDays(), couponUpdateBO.getStartTime(), couponUpdateBO.getEndTime(),
                new Date(), new Date(), false);
        Date date = new Date();
        if (marketCoupon.getTimeType() == 0) {
            Short days = marketCoupon.getDays();
            if (days == 0) {
                return BaseRespVo.invalidParameter("请输入天数");
            }
            marketCoupon.setStartTime(date);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE,days);
            marketCoupon.setEndTime(calendar.getTime());
        }else {
            if(marketCoupon.getStartTime() == null){
                marketCoupon.setStartTime(date);
            }
            if (marketCoupon.getEndTime() == null) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.add(Calendar.YEAR, 99);
                marketCoupon.setEndTime(calendar.getTime());
            }
            if(marketCoupon.getStartTime().after(marketCoupon.getEndTime())){
                return BaseRespVo.invalidParameter("开始时间不能大于结束时间");
            }
        }
        marketCouponMapper.updateByPrimaryKeySelective(marketCoupon);
        if (couponUpdateBO.getGoodsType() != null) {
            String[] strings = new String[0];
            try {
                strings = objectMapper.readValue(marketCoupon.getGoodsValue(), String[].class);
                ArrayList<String> stringList = new ArrayList<>(Arrays.asList(strings));
                return BaseRespVo.ok(new CouponUpdateVO(marketCoupon.getId(), marketCoupon.getName(), marketCoupon.getDesc(), marketCoupon.getTag(), marketCoupon.getTotal(), marketCoupon.getDiscount(),
                        marketCoupon.getMin(), marketCoupon.getLimit(), marketCoupon.getType(), marketCoupon.getStatus(), marketCoupon.getGoodsType(), stringList, marketCoupon.getCode(), marketCoupon.getTimeType(), marketCoupon.getDays(),
                        marketCoupon.getAddTime(), marketCoupon.getUpdateTime(), marketCoupon.getDeleted()));
            } catch (Exception e) {
                return BaseRespVo.invalidParameter("参数错误");
            }
        } else {
            return BaseRespVo.ok(new CouponUpdateVO(marketCoupon.getId(), marketCoupon.getName(), marketCoupon.getDesc(), marketCoupon.getTag(), marketCoupon.getTotal(), marketCoupon.getDiscount(),
                    marketCoupon.getMin(), marketCoupon.getLimit(), marketCoupon.getType(), marketCoupon.getStatus(), marketCoupon.getGoodsType(), null, marketCoupon.getCode(), marketCoupon.getTimeType(), marketCoupon.getDays(),
                    marketCoupon.getAddTime(), marketCoupon.getUpdateTime(), marketCoupon.getDeleted()));
        }
    }

    @Override
    public BaseRespVo delete(CouponDeleteBO couponDeleteBO) {
        MarketCoupon marketCoupon = new MarketCoupon();
        marketCoupon.setId(couponDeleteBO.getId());
        marketCoupon.setDeleted(true);
        marketCoupon.setUpdateTime(new Date());
        int i = marketCouponMapper.updateByPrimaryKeySelective(marketCoupon);
        if (i > 0) {
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.invalidParameter("删除失败");
        }
    }

    @Override
    public BaseRespVo listuser(BaseParam baseParam, Integer couponId) {
        List<MarketCouponUser> list = new ArrayList<>();
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = marketCouponUserExample.createCriteria();
        criteria.andCouponIdEqualTo(couponId);
        marketCouponUserExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        list = marketCouponUserMapper.selectByExample(marketCouponUserExample);
        PageInfo<MarketCouponUser> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return BaseRespVo.ok(new CouponUserListVO(baseParam.getPage(),baseParam.getLimit(),total, pages, list));
    }

    @Override
    public BaseRespVo read(Integer id) {
        MarketCoupon marketCoupon = new MarketCoupon();
        marketCoupon = marketCouponMapper.selectByPrimaryKey(id);
        if(!"[]".equals(marketCoupon.getGoodsValue())){
            String[] strings = new String[0];
            try {
                strings = objectMapper.readValue(marketCoupon.getGoodsValue(), String[].class);
                ArrayList<String> stringList = new ArrayList<>(Arrays.asList(strings));
                return BaseRespVo.ok(new CouponCreateVO(marketCoupon.getId(), marketCoupon.getName(), marketCoupon.getDesc(), marketCoupon.getTag(), marketCoupon.getTotal(), marketCoupon.getDiscount(),
                        marketCoupon.getMin(), marketCoupon.getLimit(), marketCoupon.getType(), marketCoupon.getStatus(), marketCoupon.getGoodsType(), stringList, marketCoupon.getCode(), marketCoupon.getTimeType(), marketCoupon.getDays(),
                        marketCoupon.getStartTime(), marketCoupon.getEndTime(), marketCoupon.getAddTime(), marketCoupon.getUpdateTime(), marketCoupon.getDeleted()));
            } catch (IOException e) {
                return BaseRespVo.invalidParameter("参数错误");
            }
        } else {
            return BaseRespVo.ok(new CouponCreateVO(marketCoupon.getId(), marketCoupon.getName(), marketCoupon.getDesc(), marketCoupon.getTag(), marketCoupon.getTotal(), marketCoupon.getDiscount(),
                    marketCoupon.getMin(), marketCoupon.getLimit(), marketCoupon.getType(), marketCoupon.getStatus(), marketCoupon.getGoodsType(), null, marketCoupon.getCode(), marketCoupon.getTimeType(), marketCoupon.getDays(),
                    marketCoupon.getStartTime(), marketCoupon.getEndTime(), marketCoupon.getAddTime(), marketCoupon.getUpdateTime(), marketCoupon.getDeleted()));
        }
    }
}
